package si.unimb.client;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.*;

@SuppressWarnings("serial")
public class Client extends JFrame {
	
	private static final int PRIVZETI_PORT = 1337;
	private static final String PRIVZETI_ADDRESS = "192.168.10.11";
	private String address;
	private int port;
	private JButton button;
	private JTextArea outputArea;
	private JTextField inputMsg;
	private String log;
	private Socket socket;
	private DataInputStream in;
	private DataOutputStream out;
	private InputThread it;
	private String nickname;
	private short id;
	private boolean newMode = true;
	private JFrame a = this;
	
	public static void main(String[] args) {
		new Client("86.58.52.201", 34000);
	}
	
	public Client(){
		this(PRIVZETI_ADDRESS, PRIVZETI_PORT);
	}
	
	public Client(String address, int port){
		this.address = address;
		this.port = port;
		log = "";
		initGUI();
		initCLIENT();
	}
	
	public void initGUI(){
		setTitle("Client");
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		if(newMode)
			button = new JButton("Izpisi st. niti");
		else
			button = new JButton("New mode");
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.out.println("Imamo " + Thread.activeCount() + " niti!");
				if(newMode){
					newMode = false;
				}else{
					newMode = true;
				}
				initGUI();
			}
		});
		
		inputMsg = new JTextField();
		inputMsg.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){

				if(getIt().isPrvic()){
					setNickname(inputMsg.getText());
					setTitle(inputMsg.getText());
					getIt().setPrvic(false);
				}
				
				if(inputMsg.getText().equals("/dead"))
					setTitle(getNickname() + "- DEAD!");
				
				sendData(inputMsg.getText());
				
				inputMsg.setText("");
			}
		});
		
		outputArea = new JTextArea();
		outputArea.setEditable(false);
		
		c.add(button, BorderLayout.NORTH);
		c.add(new JScrollPane(outputArea), BorderLayout.CENTER);
		c.add(inputMsg, BorderLayout.SOUTH);
		
		setSize(300, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public void initCLIENT(){
		try {
			inputMsg.setText("Enter your nickname!");
			
			setSocket(new Socket(address, port));
			
			in = new DataInputStream(getSocket().getInputStream());
			setIt(new InputThread(this, in));
			Thread input = new Thread(getIt());
			input.start();
			
			out = new DataOutputStream(getSocket().getOutputStream());
			
			addText("Povezan!");
			
		} catch (UnknownHostException e) {
			addText("Server je offline!");
		} catch (IOException e) {
			addText("Server je offline!");
		}
	}
	
	public void sendDataGameMode(short id, short x, short y){
		try{
			it.ping = System.currentTimeMillis();
			out.writeShort(id);
			out.writeShort(x);
			out.writeShort(y);
		}catch(IOException e){}
	}
	
	public void sendData(String s){
		try{
			it.ping = System.currentTimeMillis();
			out.writeInt(s.getBytes().length);
			out.write(s.getBytes(), 0, s.getBytes().length);
		}catch (SocketException e1){
			addText("Prosimo da se najprej povezete!");
		}catch (IOException e1){
			addText("Napaka pri prenosu podatkov!");
		}
	}
	
	public void addText(String msg){
		log += msg + "\n";
		
		outputArea.setText(log);
		outputArea.selectAll();
	}
	
	public void setSocket(Socket s){
		socket = s;
	}

	public Socket getSocket() {
		return socket;
	}
	
	public DataOutputStream getOutputStream(){
		return out;
	}

	public void setIt(InputThread it) {
		this.it = it;
	}

	public InputThread getIt() {
		return it;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getNickname() {
		return nickname;
	}

	public void setId(short id) {
		this.id = id;
	}
	
	public short getId(){
		return id;
	}

	public void setA(JFrame a) {
		this.a = a;
	}

	public JFrame getA() {
		return a;
	}

}
